﻿#include "mainwidget.h"
#include "ui_mainwidget.h"

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    ui->edit_CommunicationState->setText(SecsEnum::getStrCommunicationState(SecsEnum::NOT_ENABLED));
    ui->edit_ConnectionState->setText(SecsEnum::getStrConnectionState(SecsEnum::NOT_CONNECTED));
    ui->edit_ControlState->setText(SecsEnum::getStrControlState(SecsEnum::EQUIPMENT_OFFLINE));

    UiUtil::setEnabled(false, ui->btn_S1F19, ui->btn_S2F11, ui->btn_S2F17);

    /*
     * 限制用户在Port栏中的输入,必须是一个有效范围在0到65535之间的端口号
     */
    QRegularExpression ipRegex("^([0-5]?[0-9]{1,4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$");
    QRegularExpressionValidator *ipValidator = new QRegularExpressionValidator(ipRegex, ui->edit_Port);
    ui->edit_Port->setValidator(ipValidator);


    equipment = new PassiveEquipment();

    /*
     * Equipment对象创建后,需要监听他的一系列信号
     * 用于更新XXXState栏中的内容和MessageLog中的内容
     */
    connect(equipment, &PassiveEquipment::SendMsg,
            this, [=](QString message){
        ui->text_MessageLog->append(message
                                        .toHtmlEscaped()
                                        .replace("\n", "<br>")
                                        .replace(" ", "&nbsp;")
                                        .replace("&gt;", ">"));
        ui->text_MessageLog->append("--------------------");
    });

    connect(equipment, &PassiveEquipment::CommunicationStateChange,
            this, [=](SecsEnum::CommunicationState communicationstate){
        ui->edit_CommunicationState->setText(SecsEnum::getStrCommunicationState(communicationstate));
    });

    connect(equipment, &PassiveEquipment::ConnectionStateChange,
            this, [=](SecsEnum::ConnectionState connectionstate){
        ui->edit_ConnectionState->setText(SecsEnum::getStrConnectionState(connectionstate));
    });

    connect(equipment, &PassiveEquipment::ControlStateChange,
            this, [=](SecsEnum::ControlState controlstate){
        ui->edit_ControlState->setText(SecsEnum::getStrControlState(controlstate));
    });
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::on_btn_ClearMessageLog_clicked()
{
    ui->text_MessageLog->clear();
}


void MainWidget::on_btn_BindPort_clicked()
{
    int port = ui->edit_Port->text().toInt();
    equipment->BindPort(port);
    ui->btn_BindPort->setEnabled(false);
}


void MainWidget::on_btn_S1F1_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 1, 1);
}


void MainWidget::on_btn_S1F13_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 1, 13);
}


void MainWidget::on_btn_S1F19_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 1, 19);
}


void MainWidget::on_btn_S2F1_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 2, 1);
}


void MainWidget::on_btn_S2F3_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 2, 3);
}


void MainWidget::on_btn_S2F5_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 2, 5);
}


void MainWidget::on_btn_S2F11_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 2, 11);
}


void MainWidget::on_btn_S2F17_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 2, 17);
}


void MainWidget::on_btn_S2F25_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 2, 25);
}


void MainWidget::on_btn_S5F1_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 5, 1);
}


void MainWidget::on_btn_S5F9_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 5, 9);
}


void MainWidget::on_btn_S5F11_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 5, 11);
}


void MainWidget::on_btn_S5F15_clicked()
{
    equipment->SendPrimaryMessage(1, 1, 5, 15);
}


void MainWidget::on_btn_About_clicked()
{
    aboutWidget->show();
}


void MainWidget::on_btn_SelectReq_clicked()
{
    QByteArray message = MessageUtil::Select_Req(1, 1);
    equipment->SendControlMessage(message);
}


void MainWidget::on_btn_DeselectReq_clicked()
{
    QByteArray message = MessageUtil::DeSelect_Req(1, 1);
    equipment->SendControlMessage(message);
}


void MainWidget::on_btn_LinkTestReq_clicked()
{
    QByteArray message = MessageUtil::Link_Test_Req(1);
    equipment->SendControlMessage(message);
}


void MainWidget::on_btn_DisConnect_clicked()
{
    equipment->DisConnect();
    ui->edit_Port->clear();
    ui->btn_BindPort->setEnabled(true);
}

